import numpy as np
def signal(*args):
    df = args[0]
    n = args[1]
    factor_name = args[2]

    """
        https://bbs.quantclass.cn/thread/8329
         3均线趋势过滤器
          param:
            n: 短周期
            m, 中周期
            k, 长周期
    """

    df['n_ma'] = df['close'].rolling(window=n).mean()
    # ===计算信号
    df[factor_name] = 0
    df.loc[(df['open'] > df['n_ma']) & (df['n_ma'] > df['n_ma'].shift(1)), factor_name] = 1
    df.loc[(df['open'] < df['n_ma']) & (df['n_ma'] < df['n_ma'].shift(1)), factor_name] = -1

    del df["n_ma"]

    return df
